﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using _03.作用域与对象释放行为_IDisposable对象释放的时机.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace _03.作用域与对象释放行为_IDisposable对象释放的时机.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        //[HttpGet]
        //public int Get([FromServices]IOrderService orderService,
        //    [FromServices]IOrderService orderService2,
        //    [FromServices]IHostApplicationLifetime hostApplicationLifetime,
        //    [FromQuery]bool stop = false)
        //{
        //    #region 
        //    //Console.WriteLine("============1=============");
        //    //using (IServiceScope scope = HttpContext.RequestServices.CreateScope())
        //    //{
        //    //    var service = scope.ServiceProvider.GetService<IOrderService>();
        //    //    var service2 = scope.ServiceProvider.GetService<IOrderService>();
        //    //}
        //    //Console.WriteLine("=================2=============");
        //    #endregion

        //    #region 
        //    if (stop)
        //    {
        //        //将当前应用程序关闭来查看,应用程序在关闭的时候,会不会把单例的对象释放掉
        //        hostApplicationLifetime.StopApplication();
        //    }
        //    #endregion


        //    Console.WriteLine("接口请求处理结束");
        //    return 1;
        //}


        //IServiceScope 作用域
        [HttpGet]
        public int Get([FromServices]IHostApplicationLifetime hostApplicationLifetime,[FromQuery]bool stop = false)
        {
            #region 
            //Console.WriteLine("============1=============");
            //using (IServiceScope scope = HttpContext.RequestServices.CreateScope())
            //{
                  ////子容器获取对象
            //    var service = scope.ServiceProvider.GetService<IOrderService>();
            //    var service2 = scope.ServiceProvider.GetService<IOrderService>();
            //}
            //Console.WriteLine("=================2=============");
            #endregion

            #region 
            if (stop)
            {
                //将当前应用程序关闭来查看,应用程序在关闭的时候,会不会把单例的对象释放掉
                hostApplicationLifetime.StopApplication();
            }
            #endregion


            Console.WriteLine("接口请求处理结束");
            return 1;
        }


    }
}
